package edu.gatech.cs2340.thecyclonesquad;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import edu.gatech.cs2340.thecyclonesquad.database.Database;
import edu.gatech.cs2340.thecyclonesquad.models.InMemoryModel;
import edu.gatech.cs2340.thecyclonesquad.models.Person;
import android.os.Bundle;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class SignInActivity extends Activity {
	
	public final static String REGISTRATION_MESSAGE = "edu.gatech.cs2340.thecyclonesquad.USERNAMED";
	public final static String WELCOME_MESSAGE = "edu.gatech.cs2340.thecyclonesquad.USERNAMEE";
	public final static String LOCKED_MESSAGE = "edu.gatech.cs2340.thecyclonesquad.USERNAMEF";
	public final static String INCORRECT_MESSAGE = "edu.gatech.cs2340.thecyclonesquad.USERNAMEG";
	public final static String NEED_MESSAGE = "edu.gatech.cs2340.thecyclonesquad.USERNAMEH";

	

	@Override
	/**
	 * Initializes our activity.
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_in);
	}
	
	protected void onStart() {
		super.onStart();
		findViewById(R.id.username).requestFocus();
	}
	
	@Override
	/**
	 * Initialize the contents of the Activity's standard options menu.
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_sign_in, menu);
		return true;
	}
	
	/**
	 * This method registers and adds user to system.
	 * @param view is the basic building block for UI components
	 */
	public void user_register(View view) {
		EditText editText = (EditText) findViewById(R.id.username);
		String username = editText.getText().toString();
		EditText editText2 = (EditText) findViewById(R.id.password);
		String password = editText2.getText().toString();
		
		//new M9 data
		Database info = new Database(this);
		info.open();
		boolean exists = info.isThere(username);
		info.close();
		if (exists) {
			//message is printed that the user already exists
			Intent intent = new Intent(this, NowRegisteredActivity.class);
			intent.putExtra(REGISTRATION_MESSAGE, "false");
			startActivity(intent);
		}
		

		if (!(username.equals("")) && !(password.equals("")) && (!exists)) {
			//new M9 code
			Database entry = new Database(SignInActivity.this);
			entry.open();
			entry.createPerson(username, password, "false");
			entry.close();
			//message is printed that the user is now registered
			Intent intent = new Intent(this, NowRegisteredActivity.class);
			intent.putExtra(REGISTRATION_MESSAGE, username);
			startActivity(intent);

		}
	}

	/**
	 * This method is executed when user clicks the login button.
	 * @param view is the basic building block for UI components
	 */
	public void user_login(View view) {
		EditText editText = (EditText) findViewById(R.id.username);
		String username = editText.getText().toString();
		EditText editText2 = (EditText) findViewById(R.id.password);
		String password = editText2.getText().toString();
		//new M9 data
		Database info = new Database(this);
		info.open();
		boolean exists = info.isThere(username);
		if (!exists) {
			Intent intent = new Intent(this, NeedRegistrationActivity.class);
			intent.putExtra(NEED_MESSAGE, username);
			startActivity(intent);
		}
		
		if (exists) {

			int verification = info.verify(username, password);
			if (verification == 1) {
				InMemoryModel.currentUser = username;
				Intent intent = new Intent(this, AdminActivity.class);
				startActivity(intent);
			} else if (verification == 2){
				InMemoryModel.currentUser = username;
				Intent intent = new Intent(this, UserHomeActivity.class);
				intent.putExtra(WELCOME_MESSAGE, username);
				startActivity(intent);
			} else if (verification == 3) {
				Intent intent = new Intent(this, LockedOutActivity.class);
				intent.putExtra(LOCKED_MESSAGE, username);
				startActivity(intent);
			} else if (verification == 4) {
				Intent intent = new Intent(this, IncorrectLoginActivity.class);
				intent.putExtra(INCORRECT_MESSAGE, username);
				startActivity(intent);
			}
		}
		info.close();

	}
}
